﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Service;
using MODEL;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using Microsoft.Practices.ServiceLocation;
using GalaSoft.MvvmLight.Views;
using Common;
using System.ComponentModel;
using GalaSoft.MvvmLight.Ioc;

namespace ViewModel.StationManager
{
    public class StationElementConfigViewModel : ViewModelBase
    {

        public ICommand SaveCommand { get; set; }

        private StationInfo m_StationInfo = new StationInfo();
        public StationInfo StationInfo
        {
            get { return this.m_StationInfo; }
            set { this.m_StationInfo = value; base.RaisePropertyChanged(() => this.StationInfo); }
        }

        private List<ElementInfo> m_ElementList;
        public List<ElementInfo> ElementList
        {
            get { return this.m_ElementList; }
            set { this.m_ElementList = value; base.RaisePropertyChanged(() => ElementList); }
        }

        private IElementService m_ElementService;
        private IStationServcie m_StationService;

        public StationElementConfigViewModel(IElementService elementService, IStationServcie stationServcie)
        {
            this.m_ElementService = elementService;
            this.m_StationService = stationServcie;
            this.SaveCommand = new RelayCommand(SaveElementConfig);

            this.PropertyChanged += OnPropertyChange;
        }

        public async void OnPropertyChange(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "StationInfo")
            {
                try
                {
                    IEnumerable<ElementInfo> elementInfoList = await this.m_ElementService.QueryElementsAsync(this.StationInfo.StationType);
                    foreach (ElementInfo element in elementInfoList)
                    {
                        if (this.StationInfo.IsContainsElement(element.ElementCode))
                        {
                            element.IsChecked = true;
                        }
                    }

                    this.ElementList = elementInfoList.ToList();
                }
                catch (Exception ex)
                {
                    await ServiceLocator.Current.GetInstance<IDialogService>().ShowError("要素信息加载失败 ", "错误", "确定", null);
                    Log.WriteException(ex);
                }
            }
        }

        public async void SaveElementConfig()
        {
            if (this.ElementList.SafeCheck())
            {
                return;
            }
            try
            {
                List<string> checkedElementCodeList = this.ElementList.Where(element => element.IsChecked).Select(element => element.ElementCode).ToList();
                await this.m_StationService.UpdateStationElementListAsync(this.StationInfo.StationId, checkedElementCodeList);
            }
            catch (Exception ex)
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowError("保存配置失败 ", "错误", "确定", null);
                Log.WriteException(ex);
            }
        }
    }
}
